home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 2 / AACD 2.iso / AACD / Online / Socks5 / src / server / info.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-03-10  |  6.6 KB  |  152 lines

  1. /* Copyright (c) 1995-1999 NEC USA, Inc.  All rights reserved.               */
  2. /*                                                                           */
  3. /* The redistribution, use and modification in source or binary forms of     */
  4. /* this software is subject to the conditions set forth in the copyright     */
  5. /* document ("Copyright") included with this distribution.                   */
  6.  
  7. /*
  8.  * $Id: info.c,v 1.42.4.4 1999/02/03 22:35:35 steve Exp $
  9.  */
  10.  
  11. /* This file has the code for dealing with the global protocol variables.    */
  12. #include "socks5p.h"
  13. #include "threads.h"
  14. #include "daemon.h"
  15. #include "validate.h"
  16. #include "info.h"
  17. #include "log.h"
  18.  
  19. /* Return a malloc'd string representing the closest thing you can get to a  */
  20. /* Hostname for ia...                                                        */
  21. /*                                                                           */
  22. /* Arguments: ia, the address whose name we are looking up...                */
  23. void GetName(char *name, S5NetAddr *addr) {
  24.     const char *res = NULL, *eres;
  25.     const struct hostent *hp = NULL;
  26.  
  27.     memset(name, 0, S5_HOSTNAME_SIZE);
  28.  
  29.     if (addr->sa.sa_family == AF_S5NAME) {
  30.         strncpy(name, addr->sn.sn_name, MIN(strlen(addr->sn.sn_name)+1, S5_HOSTNAME_SIZE));
  31.         if (strlen(addr->sn.sn_name)+1 > S5_HOSTNAME_SIZE) name[S5_HOSTNAME_SIZE-1] = '\0';
  32.     return;
  33.     }
  34.  
  35.     MUTEX_LOCK(env_mutex);
  36.     eres = getenv("SOCKS5_REVERSEMAP");
  37.     MUTEX_UNLOCK(env_mutex);
  38.     
  39.     if (eres) {
  40.     MUTEX_LOCK(gh_mutex);
  41.     hp = gethostbyaddr((char *)&addr->sin.sin_addr, sizeof(struct in_addr), AF_INET);
  42.     if (!hp) { MUTEX_UNLOCK(gh_mutex); }
  43.     else res = hp->h_name;
  44.     }
  45.  
  46.     if (res != NULL || (res = lsAddr2Ascii(addr)) != NULL) {
  47.         strncpy(name, res, MIN(strlen(res)+1, S5_HOSTNAME_SIZE));
  48.         if (strlen(res)+1 > S5_HOSTNAME_SIZE) name[S5_HOSTNAME_SIZE-1] = '\0';
  49.     }
  50.  
  51.     if (!hp) return;
  52.     MUTEX_UNLOCK(gh_mutex);
  53. }
  54.  
  55. /* Returns a malloc'd string representing the closest then you can get to a  */
  56. /* Service name for port.                                                    */
  57. /*                                                                           */
  58. /* Arguments: port, the port whose service name we are looking up...         */
  59. void GetServ(char *name, u_short port, char *proto) {
  60.     struct servent *srv;
  61.     char *tmp;
  62.  
  63.     memset(name, 0, S5_APPNAME_SIZE);
  64.  
  65.     MUTEX_LOCK(env_mutex);
  66.     tmp = getenv("SOCKS5_SERVICENAME");
  67.     MUTEX_UNLOCK(env_mutex);
  68.  
  69.     MUTEX_LOCK(gs_mutex);
  70.     if (tmp && (srv = getservbyport(port, proto))) {
  71.     strncpy(name, srv->s_name, MIN(strlen(srv->s_name)+1, S5_APPNAME_SIZE));
  72.     if (strlen(srv->s_name)+1 > S5_APPNAME_SIZE) name[S5_APPNAME_SIZE-1] = '\0';
  73.     } else {
  74.     sprintf(name, "%d", (int)ntohs(port));
  75.     }
  76.     MUTEX_UNLOCK(gs_mutex);
  77. }
  78.  
  79. /* Resolves any hosts/services which were passed by name instead of by       */
  80. /* address, if there are any...Also it finds out which socks server to use   */
  81. /* and logs an error if it cannot pass the name to the next one (i.e. the    */
  82. /* next one is not a socks5 serve).                                          */
  83. /*                                                                           */
  84. /* Globals Used/Affected: dstsin -- filled in with destination address...    */
  85. /*                        scksin -- filled in with server address...         */
  86. int ResolveNames(S5LinkInfo *pri) {
  87.     S5NetAddr tmp;
  88.  
  89.     S5LogUpdate(S5LogDefaultHandle, S5_LOG_DEBUG(10), 0, "Resolve Names: Starting");
  90.  
  91.     pri->retName[0] = '\0';
  92.     memset((char *)&pri->retAddr, 0, sizeof(pri->retAddr));
  93.     pri->retAddr.sa.sa_family = AF_INET;
  94.     pri->retAddr.sin.sin_addr.s_addr = INVALIDADDR;
  95.     
  96.     /* If the destination address is of type S5NAME, copy the name to        */
  97.     /* pri->retAddr, resolve the name and copy the reslved address to both   */
  98.     /* pri->dstAddr and pri->retAddr...                                      */
  99.     /*                                                                       */
  100.     /* If the destination address is of type INET, copy the address to       */
  101.     /* pri->retAddr, reverse map the name and copy the mapped name to        */
  102.     /* pri->retName...                                                       */
  103.     if (pri->dstAddr.sa.sa_family == AF_S5NAME) {
  104.     S5LogUpdate(S5LogDefaultHandle, S5_LOG_DEBUG(10), 0, "Resolve names: Unresolved host name: %s", pri->dstAddr.sn.sn_name);
  105.  
  106.      strncpy(pri->retName, pri->dstAddr.sn.sn_name, MIN(strlen(pri->dstAddr.sn.sn_name) + 1, S5_HOSTNAME_SIZE));
  107.     if (strlen(pri->dstAddr.sn.sn_name) + 1 > S5_HOSTNAME_SIZE) pri->retName[S5_HOSTNAME_SIZE-1] = '\0';
  108.  
  109.     if (!lsName2Addr(pri->dstAddr.sn.sn_name, &tmp)) {
  110.         S5LogUpdate(S5LogDefaultHandle, S5_LOG_DEBUG(10), 0, "Resolve Names: host name: %s is %s", pri->dstAddr.sn.sn_name, lsAddr2Ascii(&tmp));
  111.         lsAddrSetPort(&tmp, lsAddr2Port(&pri->dstAddr));
  112.         lsAddrCopy(&pri->dstAddr, &tmp, lsAddrSize(&tmp));
  113.             lsAddrCopy(&pri->retAddr, &pri->dstAddr, lsAddrSize(&pri->dstAddr));
  114.     } else {
  115.         S5LogUpdate(S5LogDefaultHandle, S5_LOG_DEBUG(10), 0, "Resolve Names: Unresolved host name: %s still unresolved", pri->dstAddr.sn.sn_name);
  116.         lsAddrSetPort(&pri->retAddr, lsAddr2Port(&pri->dstAddr));
  117.     }
  118.  
  119.         GetName(pri->dstName, &pri->dstAddr);
  120.     } else {
  121.     if (pri->dstAddr.sin.sin_addr.s_addr != INADDR_ANY &&
  122.         !(pri->dstAddr.sin.sin_addr.s_addr & inet_addr("255.255.255.0"))) {
  123.         S5LogUpdate(S5LogDefaultHandle, S5_LOG_DEBUG(10), 0, "Resolve Names: Wrong host: %s", lsAddr2Ascii(&pri->dstAddr));
  124.         return -1;
  125.     }
  126.  
  127.         lsAddrCopy(&pri->retAddr, &pri->dstAddr, lsAddrSize(&pri->dstAddr));
  128.         GetName(pri->dstName, &pri->dstAddr);
  129.  
  130.     if (inet_addr(pri->dstName) == INVALIDADDR) strcpy(pri->retName, pri->dstName);
  131.     }
  132.  
  133.     S5LogUpdate(S5LogDefaultHandle, S5_LOG_DEBUG(10), 0, "Resolve Names: Looking up service name");
  134.     GetServ(pri->dstServ, lsAddr2Port(&pri->dstAddr), (pri->peerCommand == SOCKS_UDP)?"udp":"tcp");
  135.  
  136.     S5LogUpdate(S5LogDefaultHandle, S5_LOG_DEBUG(10), 0, "Resolve Names: Looking up next proxy");
  137.     GetProxy(&pri->dstAddr, pri->dstName, (pri->peerCommand == SOCKS_UDP)?"udp":"tcp", pri->altSckAddrs, &pri->nAltSckAddrs, &pri->nextVersion);
  138.  
  139.     if (pri->nextVersion) {
  140.     lsAddrCopy(&pri->sckAddr, &pri->altSckAddrs[0], lsAddrSize(&pri->altSckAddrs[0]));
  141.         S5LogUpdate(S5LogDefaultHandle, S5_LOG_DEBUG(10), 0, "Resolve Names: Looking up next proxy's name");
  142.         GetName(pri->sckName, &pri->sckAddr);
  143.     S5LogUpdate(S5LogDefaultHandle, S5_LOG_DEBUG(10), 0, "Resolve Names: Next Proxy: (%s:%d)", ADDRANDPORT(&pri->sckAddr));
  144.     } else {
  145.     memset(&pri->sckAddr, 0, sizeof(pri->sckAddr));
  146.     S5LogUpdate(S5LogDefaultHandle, S5_LOG_DEBUG(10), 0, "Resolve Names: No Next Proxy");
  147.     }
  148.     
  149.     return 0;
  150. }
  151.  
  152.